﻿using System;
using System.Data.Common;
using System.Linq;
using System.Text;

namespace EntityFrameworkVisualizer.VisualizerObjectSources.CommandFormatters
{
    /// <summary>
    /// Default implementation of <see cref="DbCommandFormatter&lt;T&gt;"/>.
    /// </summary>
    internal class DefaultDbFormatter : DbCommandFormatter<DbCommand>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultDbFormatter"/> class.
        /// </summary>
        /// <param name="command">Sql command.</param>
        public DefaultDbFormatter(DbCommand command)
            : base(command)
        {
        }

        /// <summary>
        /// Formatting parameters.
        /// </summary>
        /// <returns>Formatted string.</returns>
        public override string DeclareParameters()
        {
            var stringBuilder = new StringBuilder();

            foreach (var param in Command.Parameters.OfType<DbParameter>())
            {
                stringBuilder.AppendFormat("-- {0} = {1}{2}", param.ParameterName, param.Value, Environment.NewLine);
            }

            return stringBuilder.ToString();
        }

        /// <summary>
        /// Formatting stored procedure.
        /// </summary>
        /// <returns>Formatted string.</returns>
        public override string FormatStoredProcedure()
        {
            return DeclareParameters() + Command.CommandText;
        }
    }
}
